package com.bringway.p2p.controller;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.domain.SysAttechment;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.framework.util.ftp.FtpUtil;
import com.bringway.global.service.SysAttechmentService;
import com.bringway.p2p.util.Des;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/p2pContanct")
public class P2pContanctController extends JsonController{
	
	@Autowired
	private SysAttechmentService attechmentService;
	
	private static final long UPLOAD_FILE_SIZE =30971520;
	
	Logger logger = Logger.getLogger(P2pContanctController.class);
	
	@RequestMapping("/getZqPicInfos")
	public void getZqInfoPics(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String bizId = request.getParameter("bizId");
		Collection list = attechmentService.listAllAttByBizId(bizId);
		List resultList = new ArrayList();
		for(Iterator it = list.iterator();it.hasNext();){
			SysAttechment sysAtt = (SysAttechment)it.next();
			if(sysAtt.getContextType().equals("image/png") || sysAtt.getContextType().equals("image/jpeg")){
				Map map = new HashMap();
				map.put("FILE_ID", sysAtt.getId()+"");
				map.put("FILE_NAME", sysAtt.getFileName());
				map.put("FILE_SIZE", sysAtt.getSize()+"");
				resultList.add(map);
			}
		}
		JSONObject result = new JSONObject();
		result.put("data", resultList);
		serialize(request, response, result);
	};

	@RequestMapping("/downloadZqPic")
	public void downloadZqPic(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String FILE_ID = request.getParameter("FILE_ID");
		SysAttechment att = attechmentService.getSysAttechment(FILE_ID);
		if(att != null){
			logger.info("downloadZqPic 获取图片FILE_ID：" + FILE_ID + "成功！");
			response.reset();
			response.setContentType(att.getContextType());
//			response.setHeader("Content-Disposition", "attachment;filename="+new String(att.getFileName().getBytes("GBK"),"ISO-8859-1"));
			FtpUtil.download(att.getFtpPath(), response.getOutputStream());
		}else{
			logger.info("downloadZqPic 获取图片FILE_ID：" + FILE_ID + "失败！");
		}
	};
}
